package cn.wolfcode.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.wolfcode.domain.*;
import cn.wolfcode.query.ConsumptionQueryObject;
import cn.wolfcode.result.JsonResult;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.service.IConsumptionItemService;
import cn.wolfcode.service.IConsumptionService;
import cn.wolfcode.service.ISystemDictionaryItemService;
import cn.wolfcode.statusenum.ConsumptionStatusEnum;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/consumption")
public class ConsumptionController {

    @Autowired
    private IConsumptionService consumptionService;
    @Autowired
    private IBusinessService businessService;
    @Autowired
    private IConsumptionItemService consumptionItemService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") ConsumptionQueryObject qo, Model model) {
        List<Business> businesses = businessService.selectAll();
        model.addAttribute("businesses", businesses);
        ConsumptionStatusEnum[] values = ConsumptionStatusEnum.values();
        model.addAttribute("statuses", values);
        PageInfo<Consumption> pageInfo = consumptionService.list(qo);
        model.addAttribute("result", pageInfo);
        return "consumption/list";
    }

    @RequestMapping("/input")
    public String input(String cno, Model model) {
        //业务大类
        List<SystemDictionaryItem> yewudalei = systemDictionaryItemService.selectByTypeId(1);
        //业务小类
        List<SystemDictionaryItem> yewuxiaolei = systemDictionaryItemService.selectByTypeId(2);
        //支付类型
        List<SystemDictionaryItem> zhifuleixing = systemDictionaryItemService.selectByTypeId(3);
        model.addAttribute("yewudalei", yewudalei);
        model.addAttribute("yewuxiaolei", yewuxiaolei);
        model.addAttribute("zhifuleixing", zhifuleixing);
        List<Business> businesses = businessService.selectAll();
        model.addAttribute("businesses", businesses);

        if (cno == null) {

        } else {
            Consumption consumption = consumptionService.selectByCno(cno);
            model.addAttribute("currentConsumption", consumption);
            List<ConsumptionItem> items = consumptionItemService.selectByCno(cno);
            model.addAttribute("consumptionItems", items);
        }

        return "consumption/input";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Consumption consumption, Long[] category_id,
                               Long[] category_item_id, Long[] pay_type_id,
                               @RequestParam("amount1") BigDecimal[] amount,
                               @RequestParam("discount_amount1") BigDecimal[] discount_amount,
                               @RequestParam("pay_amount1") BigDecimal[] pay_amount) {

        if (consumption.getId() == null) {
            //下半部分
            String startd = DateUtil.format(new Date(), "yyyyMMddHHmmss");
            String rand = RandomUtil.randomNumbers(5);
            String cno = startd + rand;
            consumption.setCno(cno);
            consumption.setStatus(0);
            consumption.setCreate_time(new Date());
            Subject subject = SecurityUtils.getSubject();
            consumption.setCreate_user((Employee) subject.getPrincipal());
            consumptionService.insert(consumption);

            //上半部分
            //遍历对象 生成ITEM对象 插入到ITEM表
            for (int i = 0; i < category_id.length; i++) {
                ConsumptionItem item = new ConsumptionItem(new SystemDictionaryItem(category_id[i]),
                        new SystemDictionaryItem(category_item_id[i]),
                        new SystemDictionaryItem(pay_type_id[i]),
                        amount[i],pay_amount[i],discount_amount[i]);

                item.setCreate_user(consumption.getCreate_user());
                item.setCreate_time(consumption.getCreate_time());
                item.setCno(consumption.getCno());
                consumptionItemService.insert(item);
            }
        }else {
            consumptionService.updateByPrimaryKey(consumption);
            //1.根据流水号 删除ITEM表中的记录
            consumptionItemService.deleteByCno(consumption.getCno());
            //2.再插入一条
            for (int i = 0; i < category_id.length; i++) {
                ConsumptionItem item = new ConsumptionItem(new SystemDictionaryItem(category_id[i]),
                        new SystemDictionaryItem(category_item_id[i]),
                        new SystemDictionaryItem(pay_type_id[i]),
                        amount[i],pay_amount[i],discount_amount[i]);

                item.setCreate_user(consumption.getCreate_user());
                item.setCreate_time(consumption.getCreate_time());
                item.setCno(consumption.getCno());
                consumptionItemService.insert(item);
            }
        }


        return "redirect:/consumption/list";

    }

    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        consumptionService.deleteByPrimaryKey(id);
        return JsonResult.success(null);
    }
}
